import { Inject, Provide } from '@midwayjs/decorator';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { Context } from '@midwayjs/koa';
import { UserEntity } from '../entity/user';
import { SchoolEntity } from '../entity/school';

/**
 * 用户
 */
@Provide()
export class UserService extends BaseService {
    @InjectEntityModel(UserEntity)
    userEntity: Repository<UserEntity>;

    @InjectEntityModel(SchoolEntity)
    schoolEntity: Repository<SchoolEntity>;

    @Inject()
    ctx: Context;

    /**
     * 用户信息
     * @param id 用户id
     */
    async getUserInfo(id?) {
        const sql = `
            SELECT
                a.id, a.name, a.sex, a.nickname, a.username, a.mobile, a.avatar, a.college, a.class, a.remind, a.status, a.schoolId, a.expire,
                b.name as schoolName
            FROM
                app_user a
                LEFT JOIN app_school b ON a.schoolId = b.id
            WHERE
                a.id = ${id ? id : this.ctx.user.userId}
        `;
        const query = await this.userEntity.query(sql);
        if (!query[0]) {
            return {
                success: false,
                message: '用户不存在',
                data: null
            };
        }
        if (query[0].status === 0) {
            return {
                success: false,
                message: '用户已被禁用',
                data: null
            };
        }
        return {
            success: true,
            message: '查询成功',
            data: query[0]
        };
    }

    /**
     * 更换头像
     * @param url 头像地址
     */
    async changeAvatar(url) {
        try {
            await this.userEntity.update({ id: this.ctx.user.userId }, { avatar: url });
            return {
                success: true,
                message: '更换成功',
                data: null
            };
        } catch (e) {
            return {
                success: false,
                message: '更换失败',
                data: null
            };
        }
    }

    /**
     * 修改昵称
     * @param nickname 昵称
     */
    async changeNickname(nickname) {
        try {
            await this.userEntity.update({ id: this.ctx.user.userId }, { nickname });
            return {
                success: true,
                message: '修改成功',
                data: null
            };
        } catch (e) {
            return {
                success: false,
                message: '修改失败',
                data: null
            };
        }
    }

    /**
     * 修改手机号
     * @param mobile 手机号
     */
    async changeMobile(mobile) {
        try {
            await this.userEntity.update({ id: this.ctx.user.userId }, { mobile });
            return {
                success: true,
                message: '修改成功',
                data: null
            };
        } catch (e) {
            return {
                success: false,
                message: '修改失败',
                data: null
            };
        }
    }

    /**
     * 查询创建用户
     * @param openid 用户openid
     */
    async findOrCreateUser(openid) {
        const { name } = await this.userEntity.findOneBy({ openid });
        return name;
    }
}
